#pragma once

#include <string>
#include <vector>
#include <memory>
#include <fstream>
#include <sstream>
#include "market_data/market_data.h"

// 数据加载器基类
class DataLoader {
public:
    // 构造函数
    DataLoader() = default;
    
    // 析构函数
    virtual ~DataLoader() = default;
    
    // 加载数据
    virtual std::vector<MarketData> load_data(const std::string& symbol, 
                                             const std::string& start_time, 
                                             const std::string& end_time) = 0;
};

// CSV数据加载器
class CSVDataLoader : public DataLoader {
public:
    // 构造函数
    explicit CSVDataLoader(const std::string& file_path) : file_path_(file_path) {}
    
    // 加载数据
    std::vector<MarketData> load_data(const std::string& symbol, 
                                     const std::string& start_time, 
                                     const std::string& end_time) override {
        std::vector<MarketData> data;
        std::ifstream file(file_path_);
        
        if (!file.is_open()) {
            throw std::runtime_error("Failed to open file: " + file_path_);
        }
        
        std::string line;
        // 跳过标题行
        std::getline(file, line);
        
        while (std::getline(file, line)) {
            std::stringstream ss(line);
            std::string timestamp, symbol_str, price_str, volume_str, side_str, type_str;
            
            // 解析CSV行
            std::getline(ss, timestamp, ',');
            std::getline(ss, symbol_str, ',');
            std::getline(ss, price_str, ',');
            std::getline(ss, volume_str, ',');
            std::getline(ss, side_str, ',');
            std::getline(ss, type_str, ',');
            
            // 检查是否在时间范围内
            if (timestamp < start_time || timestamp > end_time) {
                continue;
            }
            
            // 检查是否匹配交易对
            if (!symbol.empty() && symbol_str != symbol) {
                continue;
            }
            
            // 创建市场数据对象
            MarketData market_data;
            market_data.timestamp = timestamp;
            market_data.symbol = symbol_str;
            market_data.price = std::stod(price_str);
            market_data.volume = std::stod(volume_str);
            
            // 解析订单方向
            if (side_str == "bid" || side_str == "BUY") {
                market_data.side = OrderSide::BUY;
            } else if (side_str == "ask" || side_str == "SELL") {
                market_data.side = OrderSide::SELL;
            }
            
            // 解析订单类型
            if (type_str == "market" || type_str == "MARKET") {
                market_data.type = OrderType::MARKET;
            } else if (type_str == "limit" || type_str == "LIMIT") {
                market_data.type = OrderType::LIMIT;
            } else if (type_str == "stop" || type_str == "STOP") {
                market_data.type = OrderType::STOP;
            } else if (type_str == "stop_limit" || type_str == "STOP_LIMIT") {
                market_data.type = OrderType::STOP_LIMIT;
            }
            
            data.push_back(market_data);
        }
        
        return data;
    }
    
private:
    std::string file_path_;
};